package com.xqzgj.web.controller.stock;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xqzgj.common.annotation.Log;
import com.xqzgj.common.core.controller.BaseController;
import com.xqzgj.common.core.domain.AjaxResult;
import com.xqzgj.common.enums.BusinessType;
import com.xqzgj.system.domain.StockCategory;
import com.xqzgj.system.service.IStockCategoryService;
import com.xqzgj.common.utils.poi.ExcelUtil;
import com.xqzgj.common.core.page.TableDataInfo;

/**
 * 商品类别Controller
 */
@RestController
@RequestMapping("/stock/category")
public class StockCategoryController extends BaseController {
    @Autowired
    private IStockCategoryService stockCategoryService;

    /**
     * 查询商品类别列表
     */
    @PreAuthorize("@ss.hasPermi('stock:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockCategory stockCategory) {
        startPage();
        List<StockCategory> list = stockCategoryService.selectStockCategoryList(stockCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品类别列表
     */
    @PreAuthorize("@ss.hasPermi('stock:category:export')")
    @Log(title = "商品类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StockCategory stockCategory) {
        List<StockCategory> list = stockCategoryService.selectStockCategoryList(stockCategory);
        ExcelUtil<StockCategory> util = new ExcelUtil<StockCategory>(StockCategory.class);
        return util.exportExcel(list, "商品类别数据");
    }

    /**
     * 获取商品类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stockCategoryService.selectStockCategoryById(id));
    }

    /**
     * 获取商品类别选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<StockCategory> categories = stockCategoryService.selectStockCategoryAll();
        return success(categories);
    }

    /**
     * 新增商品类别
     */
    @PreAuthorize("@ss.hasPermi('stock:category:add')")
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockCategory stockCategory) {
        if ("1".equals(stockCategoryService.checkCategoryNameUnique(stockCategory))) {
            return error("新增商品类别'" + stockCategory.getCategoryName() + "'失败，类别名称已存在");
        }
        if ("1".equals(stockCategoryService.checkCategoryCodeUnique(stockCategory))) {
            return error("新增商品类别'" + stockCategory.getCategoryCode() + "'失败，类别编码已存在");
        }
        stockCategory.setCreateBy(getUsername());
        return toAjax(stockCategoryService.insertStockCategory(stockCategory));
    }

    /**
     * 修改商品类别
     */
    @PreAuthorize("@ss.hasPermi('stock:category:edit')")
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockCategory stockCategory) {
        if ("1".equals(stockCategoryService.checkCategoryNameUnique(stockCategory))) {
            return error("修改商品类别'" + stockCategory.getCategoryName() + "'失败，类别名称已存在");
        }
        if ("1".equals(stockCategoryService.checkCategoryCodeUnique(stockCategory))) {
            return error("修改商品类别'" + stockCategory.getCategoryCode() + "'失败，类别编码已存在");
        }
        stockCategory.setUpdateBy(getUsername());
        return toAjax(stockCategoryService.updateStockCategory(stockCategory));
    }

    /**
     * 删除商品类别
     */
    @PreAuthorize("@ss.hasPermi('stock:category:remove')")
    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stockCategoryService.deleteStockCategoryByIds(ids));
    }
}